/*
#####################
# http://oka.vn
# hoangnm
#####################
*/

var delaySomethings = (function() {
    var timeDelay = 0;
    return function(callback, ms) {
        clearTimeout(timeDelay);
        timeDelay = setTimeout(callback, ms);
    }
})();
var arrKeyCode={
    BACKSPACE: 8,
    COMMA: 188,
    DELETE: 46,
    DOWN: 40,
    END: 35,
    ENTER: 13,
    ESCAPE: 27,
    HOME: 36,
    LEFT: 37,
    NUMPAD_ADD: 107,
    NUMPAD_DECIMAL: 110,
    NUMPAD_DIVIDE: 111,
    NUMPAD_ENTER: 108,
    NUMPAD_MULTIPLY: 106,
    NUMPAD_SUBTRACT: 109,
    PAGE_DOWN: 34,
    PAGE_UP: 33,
    PERIOD: 190,
    RIGHT: 39,
    SPACE: 32,
    TAB: 9,
    UP: 38
};
var lstSearchSuggest = null;
var html_suggest='' +
        '<ul class="rs lst-suggestion" id="sys_lst_suggestion" style="display: none;">' +
        '</ul>';
/*function getOffset( el ) {
    var _x = 0;
    var _y = 0;
    while( el && !isNaN( el.offsetLeft ) && !isNaN( el.offsetTop ) ) {
        _x += el.offsetLeft - el.scrollLeft;
        _y += el.offsetTop - el.scrollTop;
        el = el.offsetParent;
    }
    return { top: _y, left: _x };
}*/

$(function(){
    $.fn.showCenter=function(){
        return this.each(function(){
            var self = $(this);
            var setTopCSS, setLeftCSS;
            setTopCSS=($(window).height()-self.height())/2 - $(window).height()/10;
            setLeftCSS=($(window).width()-self.width())/2;
            self.css({"top":setTopCSS,"left":setLeftCSS});
        })
    };
    $.fn.ForceNumericOnly =
        function () {
            return this.each(function () {
                $(this).keydown(function (e) {
                    var key = e.charCode || e.keyCode || 0;
                    // allow backspace, tab, delete, arrows, numbers and keypad numbers ONLY
                    return (
                        key == arrKeyCode.BACKSPACE ||
                            key == arrKeyCode.TAB ||
                            key == arrKeyCode.DELETE ||
                            key == arrKeyCode.HOME ||
                            key == arrKeyCode.END ||
                            key == arrKeyCode.SHIFT ||
                            key == arrKeyCode.CTRL ||
                            (key >= 37 && key <= 40) ||
                            (key >= 48 && key <= 57) ||
                            (key >= 96 && key <= 105));
                });
            });
        };
    $.popupCommon=function(objSetting) {
        var settings = $.extend({
            wrapHtml:   '' +
                        '<div class="popup-common">' +
                            '<div class="overlay-bl-bg"></div>' +
                            '<div class="pop-content">' +
                                '<div class="opacity-border"></div>' +
                                '<div class="wrap-content">' +
                                    '<div class="main-content">' +
                                        'html <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> content' +
                                    '</div>' +
                                '</div>' +
                                '<img class="closePopup" src="../style/images/frontend/close.png" alt="CLOSE">' +
                            '</div>' +
                        '</div>',
            htmlContent:'<div style="padding: 10px;color: red;text-align: center;font-size: 14px;">Có lỗi xảy ra trong quá trình xử lý, vui lòng thử lại!</div>',
            attrId:'sys_popup_common',
            widthPop:'500px'
        },objSetting || {});

        var _$this = null;
        function initPopup() {
            _$this = $(settings.wrapHtml).appendTo("body");
            _$this.attr("id", settings.attrId);
            _$this.find(".main-content").first().html(settings.htmlContent);

            /*set width and position for popup*/
            var popContent = _$this.find(".pop-content").css("width", settings.widthPop);
            var setTopCSS, setLeftCSS;
            setTopCSS = ($(window).height() - popContent.height()) / 2 - $(window).height() / 10;
            setLeftCSS = ($(window).width() - popContent.width()) / 2;
            popContent.css({
                "top":setTopCSS,
                "left":setLeftCSS
            });
            _$this.fadeIn();
            onEvents();
        }
        function onEvents(){
            $("#" + settings.attrId).on("click.closePopup",".closePopup,.overlay-bl-bg",function(){
                $("#" + settings.attrId).fadeOut(function(){
                    $(this).remove();
                    $("body").off("keydown.closePopup");
                });
            });
            $("#" + settings.attrId).on("click",".main-content",function(e){
                e.stopPropagation();
            });
            $("body").on("keydown.closePopup",function(e){
                var getCode = e.keyCode ? e.keyCode : e.which;
                if(getCode == arrKeyCode.ESCAPE) {
                    $("#" + settings.attrId).find(".closePopup").trigger("click");
                }
            });
        }

        initPopup();
    };
});